#!/usr/bin/env python
"""
启动应用并测试路由的脚本
"""
import subprocess
import time
import requests
import sys
import os

def start_application():
    """启动应用"""
    print("正在启动应用...")
    
    # 使用相对路径
    script_path = os.path.join(os.path.dirname(__file__), "start.py")
    
    # 启动应用
    process = subprocess.Popen([
        sys.executable, script_path
    ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    
    # 等待应用启动
    print("等待应用启动...")
    time.sleep(10)
    
    return process

def test_routes():
    """测试路由"""
    base_url = "http://localhost:8002"
    
    # 测试路由列表
    routes_to_test = [
        "/api/data-sources",
        "/api/data-sources/supported-types",
        "/api/llm/providers",
        "/api/llm/health",
        "/health",
    ]
    
    print("开始测试路由...")
    
    for route in routes_to_test:
        url = f"{base_url}{route}"
        try:
            response = requests.get(url, timeout=5)
            print(f"{route}: {response.status_code}")
        except requests.exceptions.RequestException as e:
            print(f"{route}: 连接失败 - {e}")

def main():
    # 启动应用
    process = start_application()
    
    try:
        # 测试路由
        test_routes()
    finally:
        # 停止应用
        print("正在停止应用...")
        process.terminate()
        process.wait()

if __name__ == "__main__":
    main()